package com.qidian.mes.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qidian.mes.sys.mapper.UserMapper;
import com.qidian.mes.sys.service.UserService;
import com.qidianit.mes.common.pojo.MesResult;
import com.qidianit.mes.common.util.JwtUtil;
import com.qidianit.mes.pojo.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 戴着假发的程序员
 * @company 起点编程
 * @date 2025/8/7 9:39
 * @description
 */
@Service
@Transactional
public class UserviceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public MesResult<String> login(String username, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User user = baseMapper.selectOne(queryWrapper);
        if(user != null ){
            if(user.getPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes()))){
                // 产生token
                Map<String,Object> claims = new HashMap<>();
                claims.put("username",username);
                claims.put("id",user.getId());
                String token = JwtUtil.generateToken(claims, 30*60*1000);
                return MesResult.success(token);
            }
        }
        return MesResult.error();
    }
}
